﻿using SerieSpotter.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace SerieSpotter.Controllers
{
    public class CommentsController : LanguageController
    {
        //
        // GET: /Comments/
        SerieSpotterDb _db = new SerieSpotterDb();

        public ActionResult Index([Bind(Prefix = "id")]int episode)
        {
            var comment = _db.Episodes.Find(episode); //haalt alle comments van een episode uit de database
            if (comment != null) //controle
            {
                return View(comment);
            }
            return HttpNotFound(); 
           
        }

        //
        // GET: /Comments/Create
        [HttpGet]
       // [Authorize(Roles = "admin")]
        [Authorize(Roles = "user, admin")] // beide mogen een comment creeëren
        public ActionResult Create(int episodeId)
        {
            if (episodeId != 0)
            {
                return View();
            }
            else
            {
                return View("mislukt");
            }
  
        }

        //
        // POST: /Comments/Create
        [HttpPost]
       // [Authorize(Roles="admin")]
        [Authorize(Roles = "user, admin")]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Comment comment)
        {
            if (ModelState.IsValid)
            {
                comment.Date = DateTime.Now; // datum wanneer de post gebeurd
                comment.RepliedTo = 0;
                comment.UserProfileUserId = (int) Membership.GetUser().ProviderUserKey; // schrijf id weg van de user
                _db.Comments.Add(comment); // adden
                _db.SaveChanges(); // saven in db
                return RedirectToAction("Index", new { id = comment.EpisodeId }); // terug naar de overzichtspagina
            }
            return View(comment);
        }

      
        //
        // GET: /Comments/Edit/5
        [HttpGet]
        [Authorize(Roles = "admin")]
        public ActionResult Edit(int id)
        {
            var model = _db.Comments.Find(id); // heel gegevens op
            return View(model); // toon op scherm
        }

        //
        // POST: /Comments/Edit/5
        [HttpPost]
        [Authorize(Roles = "admin")]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Comment comment)
        {
            if (ModelState.IsValid)
            {
                comment.Date = DateTime.Now;
                comment.RepliedTo = 0;
                comment.UserProfileUserId = (int)Membership.GetUser().ProviderUserKey;
                _db.Entry(comment).State = EntityState.Modified; // er is iets veranderd!
                _db.SaveChanges(); // opslaan veranderingen
                return RedirectToAction("Index", new { id = comment.EpisodeId }); //redirect
            }
            return View(comment);
        }


        //
        // GET: /Comments/Delete/5
        public static int epId; // rederect na delete naar juist pagina
       [HttpGet]
       [Authorize(Roles = "admin")]
        public ActionResult Delete(int id)
        {
            var model = _db.Comments.Find(id); // haal gegevens op
            epId = model.EpisodeId;
            return View(model);
        }
       
            
        //
        // POST: /Comments/Delete/5
        [HttpPost]
        [Authorize(Roles = "admin")]
        [ValidateAntiForgeryToken]
       public ActionResult Delete(Comment comment)
        {
           
             if (ModelState.IsValid)
            {
                
                _db.Comments.Attach(comment);
                _db.Comments.Remove(comment);
                _db.SaveChanges();
                return RedirectToAction("Index", new { id =epId });
            }
             return View(comment);
            
        }

        //sluit de database connectie
        protected override void Dispose(bool disposing)
        {
            if (_db != null)
                _db.Dispose();
            base.Dispose(disposing);
        }
    }
}
